<%@ page language="java" contentType="text/html; charset=UTF-8"	 pageEncoding="UTF-8" %>
<%@ include file="../include/laypublic.jsp" %>
<!DOCTYPE html>
<html>
<head>
  <title>案件调解执行和解申请单</title>
</head>
<style>	
	.fieldset{
		margin-top: 5px;
		margin-bottom: 0px
	}
	.layui-legend{
		background-color:#5fb878;
		font-size:15px !important;
		width:140px;
	}
	.layui-form-item {
	    margin-bottom: 5px;
	    clear: both;
	    *zoom: 1;
	}
	.layui-form-label{width: 180px;}
</style>
<body>
<div class="layui-fluid">
    <div class="layui-card">
	    <div class="layui-tab-item layui-show">
	    	<div style="padding-top:15px;">&nbsp;</div>
			<form class="layui-form" id="addInfo_form" lay-filter="add_info_from">
				<fieldset class="layui-elem-field layui-field-title fieldset"  id="baseInfo">
				  <legend class="layui-btn layui-legend" style="text-align:left;cursor:default;">基础信息  ></legend>
				  <div style="margin-top:20px" >				  
				  	<div class="layui-form-item" >
				  		<div class="layui-inline">
					      	<label class="layui-form-label">申请公司</label>
					      	<div class="layui-input-inline">
					      		<input type="hidden" name="hrOrgId" value="${bindOrgId}">
								<input type="text" value="${bindOrgName}" style="border: 0px;" readonly="readonly" class="layui-input">
					      	</div>
					    </div>
					    <div class="layui-inline">
					    	<label class="layui-form-label" >申请部门</label>
				      		<div class="layui-input-inline" >
				      			<input type="hidden" name="hrDeptId" value="${bindDeptId}">
								<input type="text" value="${bindDeptName}" style="border:0px;" readonly="readonly" class="layui-input">
				      		</div>
					    </div>
					    <div class="layui-inline">
					    	<label class="layui-form-label">申请人</label>
				      		<div class="layui-input-inline">
				        		<input type="hidden" name="hrStaffApplyId" readonly="readonly" value="${SESSION_KEY_USER_DETAIL.hrStaffId }">
				        		<input type="text" name="staffName" value="${staffName }" readonly="readonly" style="border:0px" class="layui-input">
				      		</div>
					    </div>
				    </div>
				  	<div class="layui-form-item">
				  		<div class="layui-inline">
							<label class="layui-form-label" >原申请单类型</label>
							<div class="layui-input-inline" >
								<select name="lawcaseType" id="add_select_lawcaseType" lay-filter="add_select_lawcaseType" lay-verify="required">
		              				<option value="">请选择-原申请单类型</option>
				              		<option value="01" >起诉案件</option>
				              		<option value="02" >上诉案件</option>
				              		<option value="03" >执行案件</option>	              					
				              	</select>				              
							</div>
						</div>						
				  		<div class="layui-inline">
					      	<label class="layui-form-label">案件名称</label>
					      	<div class="layui-input-inline">
					        	<select name="refLawcaseId" id="add_select_sueLawcaseId" lay-filter="add_select_sueLawcaseId" lay-verify="required" lay-search>
		              				<option value="">请选择</option>
				              	</select>						        
								<input type="hidden" id="id_lawcaseName" name="lawcaseName">
								<input type="hidden" id="id_causeType" name="causeType">
					      	</div>
					    </div>
					    <div class="layui-inline">
							<label class="layui-form-label">和解申请日期</label>
							<div class="layui-input-inline">
								<input type="text" class="layui-input" id="id_peaceDate" name="peaceDate" autocomplete="off" lay-verify="required" readonly placeholder="和解申请日期">
							</div>
						</div>
				    </div>		
				   <div class="layui-form-item">
				  		<div class="layui-inline">
					      	<label class="layui-form-label" >标的额（万元）</label>
					      	<div class="layui-input-inline" >
					        	<input type="text" id="id_targetAmount" name="targetAmount" readonly="readonly" style="border:0px" class="layui-input">
							</div>
					    </div>
					    <div class="layui-inline">
							<label class="layui-form-label" >案由</label>
							<div class="layui-input-inline" >
								 <infopub:select entireName="-案由" id="query-select-causeType" layVerify="required" disabled="true" type="dic" groupNo="A00010078" cssClass="layui-input"></infopub:select>
							</div>
						</div>
					    <div class="layui-inline" >
							<label class="layui-form-label" >原告/上诉人/执行人</label>
							<div class="layui-input-inline" >
								<input type="text" id="id_bringFromMan" name="bringFromMan" readonly="readonly" style="border:0px" class="layui-input">
							</div>
						</div>
				    </div>				    		    		    
				    <div class="layui-form-item" >
						<div class="layui-inline" >
							<label class="layui-form-label" >被告/被上诉人/被执行人</label>
							<div class="layui-input-inline" >
								<input type="text" id="id_bringToMan" name="bringToMan" readonly="readonly" style="border:0px" class="layui-input">
							</div>
						</div>
						<div class="layui-inline" >
							<label class="layui-form-label" >起诉状/上诉书/生效法律文书</label>
							<div class="layui-input-block" >
								<div class="layui-upload" >
									<div class="layui-upload-list" id="11-upload-more-list"></div>
								</div>
							</div>
						</div>
					</div>												  	
		    		<div class="layui-form-item">
						<label class="layui-form-label" >调解/执行和解的理由与方案</label>
						<div class="layui-input-block" >
							<textarea name="peaceReason" class="layui-textarea" maxlength="1000" lay-verify="required" style="width: 899px; height: 90px;"></textarea>
						</div>
					</div>
					<div class="layui-form-item" >
						<label class="layui-form-label" >调解协议/执行和解协议</label>
						<div class="layui-input-block" >
							<textarea name="peaceAgree" class="layui-textarea" maxlength="1000" lay-verify="required" style="width: 899px; height: 90px;"></textarea>
						</div>
					</div>
		  		</div>
			</fieldset>		
			<fieldset class="layui-elem-field layui-field-title fieldset">
			<legend class="layui-btn layui-legend"
				style="text-align: left; cursor: default;">附件信息 ></legend>
			<div style="margin-top: 20px;">
				<div class="layui-form-item">
					<label class="layui-form-label" style="width: 350px;">支持pdf、excel、word、ppt、图片资料，可上传多个</label>
				</div>										
				<div class="layui-form-item">
					<label class="layui-form-label" >调解/执行和解的理由与方案</label>
					<div class="layui-input-block" >
						<div class="layui-upload" >
							<button type="button" class="layui-btn" id="01-upload-more" >上传</button>
							<div class="layui-upload-list" id="01-upload-more-list" ></div>
						</div>
					</div>
				</div>
				<div class="layui-form-item">
					<label class="layui-form-label">调解协议/执行和解协议</label>
					<div class="layui-input-block">
						<div class="layui-upload">
							<button type="button" class="layui-btn" id="02-upload-more" >上传</button>
							<div class="layui-upload-list" id="02-upload-more-list" ></div>
						</div>
					</div>
				</div>
				<div class="layui-form-item">
					<label class="layui-form-label">其他附件（选填）</label>
					<div class="layui-input-block">
						<div class="layui-upload">
							<button type="button" class="layui-btn" id="03-upload-more" >上传</button>
							<div class="layui-upload-list" id="03-upload-more-list" ></div>
						</div>
					</div>
				</div>
				</div>
			</fieldset>
						
			<div class="layui-form-item" style="position: fixed ! important; right: 30px; bottom:45%;z-index:10000;">
				<div class="layui-input-block">
		      		<button class="layui-btn layui-bg-blue layui-btn-normal layui-btn-lg  layui-btn-radius" type="button" lay-submit lay-filter="addInfo_form_submit" id="addInfo_form_submit">
		      		<i class="layui-icon layui-icon-ok layuiadmin-button-btn" ></i>
		      		提交
		      		</button>
		      	</div>
		    </div>
		    
		</form>
  		</div>
	</div>
</div>
<script>
	//静态资源路径前缀
	var staticPath = '${fileHttpPre}';
	var stateJSON = <infopub:dicJson groupNo="A00010028" />;
	
  layui.config({
    base: '${ctx}/resources/layuiadmin/' //静态资源所在路径
  }).extend({
    index: 'lib/index' //主入口模块
  }).use(['index', 'set', 'upload', 'element', 'laydate'], function(){
    var $ = layui.$
    ,form = layui.form
    ,element = layui.element
    ,laydate = layui.laydate
    ,upload = layui.upload;
    
 	// 正则校验
	form.verify({
		//验证汉字
	    CHS:[
	    	/^[\u0391-\uFFE5]+$/
	    	,'只能输入汉字'
	  	],
	  	//大于0的正整数
	  	intNumber:[
			/^[1-9]\d*$/
			,'请输入正确的数字'
	  	],
	  	//数字只能有两位小数
	  	floatNumber:[
			/^([-]?)([0-9](\d+)?(\.\d{1,2})?$)|(^\d\.\d{1,2}$)/
			,'请输入正确的数字'
	  	],
	  	//数字大于等于0，并只能有两位小数
	  	floatAmount:[
			/^([0-9](\d+)?(\.\d{1,2})?$)|(^\d\.\d{1,2}$)/
			//var pattern=/^((\d+\.\d*[1-9]\d{1})|(\d*[1-9]\d*\.\d{2}))$/; 
			,'请输入正确的数字'
	  	]
	});
 	
	laydate.render({
        elem: '#id_peaceDate'
        ,trigger: 'click'
    });
	
	var uploadRender = function(id) {
		upload.render({
			elem : '#' + id + '-upload-more',
			url : basePath+'/peaceLawcase/uploadAtt/',
			data : {
				types : id
			},
			multiple : true,
			accept : 'file',
			acceptMime : 'application/zip,application/x-rar-compressed,image/jpeg,image/png,application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/vnd.ms-powerpoint,application/vnd.openxmlformats-officedocument.presentationml.presentation,application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/pdf',
			before : function(obj) {
				layer.load();
			},
			done : function(res) {
				if (res.code > 0) {
					layer.closeAll('loading');
					return layer.msg(res.msg, {
						icon : 7
					});
				}
				if (res.entity != null&& res.entity.length > 0) {
					for ( var i in res.entity) {
						var preProject = res.entity[i];
						var arr = preProject
								.split("|");
						var imgSrc = "";
						var suffix = arr[1].substring(arr[1].lastIndexOf(".") + 1,arr[1].length);
						if (suffix == 'jpg'|| suffix == 'png') {
							imgSrc = "${fileHttpPre}/"+arr[3];
						} else {
							imgSrc = "${ctx}/resources/images/"+suffix+".jpg";
						}
						$('#'+id+'-upload-more-list').append('<div class="layui-upload-img-div" id="upload_img_'+arr[2]+'"><img src="'+imgSrc+'" title="'+ arr[1] +'" class="layui-upload-img"><div class="mask"><a href="javascript:void(0)" onclick="showImg(\''+"${fileHttpPre}"+arr[3]+'\');">查看</a><a href="javascript:void(0)" onclick="del(\''+arr[2]+'\')";>删除</a></div><p>'+arr[1]+ '</p><input type="hidden" value="'+preProject+'" id="fileAtt_'+arr[2]+'" name="fileAtt'+id+'" ></input></div>');
					}
				}
			},
			allDone : function(obj) {
				layer.closeAll('loading');
			},
			error : function() {
				layer.closeAll('loading');
				layer.msg("上传失败，请稍后再试~", {
					icon : 7
				});
			}
		});
		}

  	uploadRender('01');
  	uploadRender('02');
  	uploadRender('03');
  	
	//监听申请单类型
	form.on('select(add_select_lawcaseType)', function(data) {
		var id = data.value;
   		$("#id_targetAmount").val('');
		$("#query-select-causeType").val('');
		$("#id_lawcaseName").val('');
		$("#id_causeType").val('');
		$("#add_select_sueLawcaseId").html('');
		$("#id_bringFromMan").val('');
		$("#id_bringToMan").val('');
		$("#11-upload-more-list").html('');
   		form.render();
		if(id==''||id==null){
    		return;
    	}else{
   			getLawcaseByType(id);
    	}								
	});

  	//根据案件名称带出上诉案件申请需要的字段
	function getLawcaseByType(id) {
		var $ = layui.$;
		$.ajax({
			type : "POST", //提交方式  
			url : "${ctx}/peaceLawcase/getLawcaseByType",//路径  
			data : {
				"type" : id
			},//数据，这里使用的是Json格式进行传输  
			success : function(result) {//返回数据根据结果进行相应的处理 
				if (result.flag == true) {
					//案件名称带出案件基础信息
					$("#add_select_sueLawcaseId").html('');
					if(result.lawcases != null && result.lawcases != ''){
						var tableTemplate = [];
						tableTemplate.push('<option value>请选择</option>');
						var lawcases = result.lawcases;
						layui.$.each(lawcases ,function (index, lawcase){
							tableTemplate.push('<option value="'+lawcase.id+'">'+lawcase.lawcaseName+'</option>');
	                    });
						var $div = $("#add_select_sueLawcaseId");
	                    $div.append(tableTemplate.join(''));
					}else{
						$("#id_targetAmount").val('');
						$("#query-select-causeType").val('');
						$("#id_lawcaseName").val('');
						$("#id_causeType").val('');
						$("#add_select_sueLawcaseId").html('');
						$("#id_bringFromMan").val('');
						$("#id_bringToMan").val('');
						$("#11-upload-more-list").html('');
						layer.msg('未查询到对应的案件记录~',{icon: 0});
					}					
					form.render();
				}else{
					layer.msg('未查询到案件记录~',{icon: 0});
				}
			}
		});
	}
	//监听案件名称
	form.on('select(add_select_sueLawcaseId)', function(data) {
		var id = data.value;
		if(id==''||id==null){
    		$("#id_targetAmount").val('');
			$("#query-select-causeType").val('');
   			$("#id_lawcaseName").val('');
   			$("#id_causeType").val('');
   			$("#id_bringFromMan").val('');
   			$("#id_bringToMan").val('');
   			$("#11-upload-more-list").html('');
    		return;
    	}else{
   			var lawcaseName = $("#add_select_sueLawcaseId").find("option:selected").text().trim();
   			$("#id_lawcaseName").val(lawcaseName);
   			var lawcaseType = $("#add_select_lawcaseType").val();
   			getSueLawcaseById(id,lawcaseType);
    	}								
	});

  	//根据案件名称带出上诉案件申请需要的字段
	function getSueLawcaseById(id,type) {
		var $ = layui.$;
		$.ajax({
			type : "POST", //提交方式  
			url : "${ctx}/peaceLawcase/getLawcaseById",//路径  
			data : {
				"lawcaseId" : id,
				"type" : type
			},//数据，这里使用的是Json格式进行传输  
			success : function(result) {//返回数据根据结果进行相应的处理 
				if (result.flag == true) {
					//案件名称带出案件基础信息
					if(result.lawcase != null && result.lawcase != ''){
						$("#id_causeType").val(result.lawcase.causeType);
						//带出标的额
						if(result.lawcase.targetAmount != null && result.lawcase.targetAmount != ''){
							$("#id_targetAmount").val(result.lawcase.targetAmount);
						}else{
							$("#id_targetAmount").val('');
						}
						//带出案由
						if(result.lawcase.causeType != null && result.lawcase.causeType != ''){
							$("#query-select-causeType").val(result.lawcase.causeType);
						}else{
							$("#query-select-causeType").val('');
						}
						//原告/被告
						if(result.lawcase.plaintiff != null && result.lawcase.plaintiff != ''){
							$("#id_bringFromMan").val(result.lawcase.plaintiff);
   							$("#id_bringToMan").val(result.lawcase.defendant);
						}else{
							//上诉人/被上诉人
							if(result.lawcase.upwardMan != null && result.lawcase.upwardMan != ''){
								$("#id_bringFromMan").val(result.lawcase.upwardMan);
	   							$("#id_bringToMan").val(result.lawcase.passiveUpwardMan);
							}else{
								//执行人/被执行人
								if(result.lawcase.applyEnforcer != null && result.lawcase.applyEnforcer != ''){
									$("#id_bringFromMan").val(result.lawcase.applyEnforcer);
		   							$("#id_bringToMan").val(result.lawcase.actEnforcer);
								}else{
									$("#id_bringFromMan").val('');
		   							$("#id_bringToMan").val('');
								}
							}
						}
						form.render();
						var lawcaseAtts = [];
						var attType = '';
						$("#11-upload-more-list").html('');
						if(result.lawcase.carryLawcaseAtts != null && result.lawcase.carryLawcaseAtts.length > 0){
							lawcaseAtts = result.lawcase.carryLawcaseAtts;
							attType = '13';
						}else{
							if(result.lawcase.upwardLawcaseAtts != null && result.lawcase.upwardLawcaseAtts.length > 0){
								lawcaseAtts = result.lawcase.upwardLawcaseAtts;
								attType = '12';
							}else{
								if(result.lawcase.sueLawcaseAtts != null && result.lawcase.sueLawcaseAtts.length > 0){
									lawcaseAtts = result.lawcase.sueLawcaseAtts;
									attType = '11';
								}
							}
						}
						layui.$.each(lawcaseAtts, function(i,item){
							var imgSrc = "";
							var suffix = item.fileSuffix.substring(item.fileSuffix.lastIndexOf(".") + 1,item.fileSuffix.length);
							if(suffix == 'jpg' || suffix == 'png'){
								imgSrc = "${fileHttpPre}/"+item.filePath;
							}else{
								imgSrc = "${ctx}/resources/images/"+suffix+".jpg";
							}
							if(item.types == result.attTypes){//获取对应的资料类型
			        			$('#11-upload-more-list').append('<div class="layui-upload-img-div" "><img src="'+imgSrc+'" title="'+ item.fileName +'" class="layui-upload-img"><div class="mask"><a href="javascript:void(0)" style="margin-left:32px" onclick="showImg(\''+"${fileHttpPre}"+item.filePath +'\');">查看</a></div><p>'+item.fileName+'</p><input type="hidden" value="'+attType+'|'+item.fileName+item.fileSuffix+'|'+item.id+'|'+item.filePath+'" name="fileAtt'+attType+'" ></input></div> ');
							}
						});
					}else{
						$("#id_targetAmount").val('');
						$("#id_lawcaseName").val('');
						$("#id_causeType").val('');
						$("#query-select-causeType").val('');
						$("#id_bringFromMan").val('');
						$("#id_bringToMan").val('');
						$('#11-upload-more-list').html('');
						form.render();
					}					
				}else{
					layer.msg('未查询到上诉案件或起诉案件记录~',{icon: 0});
				}
			}
		});
	}
   	//监听提交
  	form.on('submit(addInfo_form_submit)', function(data){
  		//判断是否上传了调解/执行和解的理由与方案
		if ($("input[name=fileAtt01]").length < 1) {
			layer.msg("调解/执行和解的理由与方案未上传", {
				icon : 0
			});
			return;
		}
		//判断是否上传了调解协议/执行和解协议
		if ($("input[name=fileAtt02]").length < 1) {
			layer.msg("调解协议/执行和解协议未上传", {
				icon : 0
			});
			return;
		}
  		$("#addInfo_form_submit").removeAttr("lay-submit").removeClass("layui-bg-blue").addClass("layui-btn-disabled");
  		layer.load();
       	//提交 Ajax 成功后，静态更新表格中的数据
       	$.ajax({
           	type : 'post',
			url : basePath + '/peaceLawcase/save',
           	data: $('#addInfo_form').serialize(),
			success:function(result){
				if(result.success){
					layer.closeAll('loading'); //关闭loading
					layer.msg('提交单据成功~',{icon: 1});
					//两秒后关闭当前页面
					setTimeout(function(){
						/**
						 * 查找父菜单，并刷新
						 */
						var topDoc = $(window.top.document);
						var path = basePath + '/peaceLawcase/list';
						// 获取页面名称
						var name = topDoc.find(".layui-nav-item [lay-href^='" + path + "']").text();
						if(name == '' || name == null){
							name = '案件调解执行和解';
						}
						window.parent.layui.index.openTabsPage(path, name);
						var iframe = window.parent.layui.admin.tabsBody(window.parent.layui.admin.tabsPage.index).find(".layadmin-iframe");
						iframe[0].contentWindow.location.reload(true);
						
						/**
						 * 关闭当前页
						 */
						var currPath = basePath + '/peaceLawcase/add';
						var layid = topDoc.find("li[lay-id^='" + currPath + "']").attr('lay-id');
						window.parent.layui.element.tabDelete('layadmin-layout-tabs', layid);//关闭当前页
				        }, 2000);
				}else{
					layer.closeAll('loading'); //关闭loading
  					$("#addInfo_form_submit").removeClass("layui-btn-disabled").addClass("layui-bg-blue").attr("lay-submit","");
					layer.msg(result.msg,{icon: 7});
			  	}
			}
       	});
   	});  
   	
 	// data-type事件
    $('.layui-btn.layuiadmin-btn-admin').on('click', function(){
      var type = $(this).data('type');
      active[type] ? active[type].call(this) : '';
    });  	
  });
  
	//删除文件
   function del(index){
	 layui.$("#upload_img_"+index).remove();
   }
  
	//根据url查看文件
	function showImg(url){
		var openWin = window.open(url,'','left=410,height=768, width=1024, top=200,  toolbar=no, menubar=no, scrollbars=no, resizable=no,location=no, status=no');
	}
	
  </script>
</body>
</html>